package com.example.client_anna.module;

import androidx.appcompat.app.ActionBar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.client_anna.R;
import com.example.client_anna.base.ActivityCollector;
import com.example.client_anna.base.BaseActivity;
import com.example.client_anna.network.okhttp3.HttpUtilImpl;
import com.example.client_anna.network.okhttp3.RequestUrl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
//注册模块
public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    private static final String TAG = "RegisterActivity";

    private EditText editTextname;
    private EditText editTextpasswd;
    private EditText editTextrepasswd;

    private String backjson = "";
    private Boolean aBoolean = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        editTextname=(EditText)findViewById(R.id.register_username);
        editTextpasswd=(EditText)findViewById(R.id.register_userpasswd);
        editTextrepasswd=(EditText)findViewById(R.id.register_repasswd);

        Button button=(Button)findViewById(R.id.register_submit);
        button.setOnClickListener(this);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        //半透明状态栏
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //隐藏状态栏
        //View decorView = window.getDecorView();
        //decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_submit:
                if("".equals(editTextname.getText().toString())||"".equals(editTextpasswd.
                        getText().toString())||"".equals(editTextrepasswd.getText().toString())){
                    Toast.makeText(RegisterActivity.this,"信息填写不完整",Toast.
                            LENGTH_SHORT).show();
                }else {
                    if(!(editTextpasswd.getText().toString().equals(editTextrepasswd.getText().
                            toString()))){
                        Toast.makeText(RegisterActivity.this,"两次密码填写不一致",Toast.
                                LENGTH_SHORT).show();
                        editTextrepasswd.setText("");
                    }else {
                        Register_Okhttp3();
                    }
                }
                break;
        }
    }

    private void Register_Okhttp3() {
        //链接的地址
        String address = RequestUrl.getUrl()+"user";
        //创建RequesstBody请求体,用于数据的提交
        RequestBody requestBody=new FormBody.Builder()
                .add("id","002")
                .add("username",editTextname.getText().toString())
                .add("userpasswd",editTextpasswd.getText().toString())
                .build();
        //调用HttpUtil网络请求，通过okhttp3进行网络访问
        HttpUtilImpl.sendOKHttpRequest(address,requestBody,new okhttp3.Callback(){
            @Override
            public void onResponse(Call call, Response response)throws IOException {
                //onResponse方法用于接收访成功后回调后的操作
                //得到response对象的具体内容
                String responseData=response.body().string();
                //分析返回的数据
                ParesJsonWithGSON(responseData);
            }
            @Override
            public void onFailure(Call call, IOException e) {
                //onFailure方法用于网络请求失败后执行的语句
                //取消请求的判断，true用户主动关闭活动页面，false超时或者网络出错
                if(e.toString().contains("closed")){
                    //用户直接关闭页面后取消请求
                    call.cancel();
                }else{
                    //网络不稳定，或者无网络
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this,
                                    "网络出错，请等待！", Toast.LENGTH_SHORT).show();
                        }
                    });
                    //取消请求
                    call.cancel();
                }

            }
        });
    }

    private void ParesJsonWithGSON(String jsonData){

        Gson gson = new Gson();
        //字符串使用.equals判断
        if(!("".equals(jsonData))) {
            Map<String, String> map = gson.fromJson(jsonData, new TypeToken<Map<String, String>>(){
            }.getType());

            for (String value : map.values()) {
                backjson = value;
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    switch (backjson){
                        case "用户名已存在":
                            Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.
                                    LENGTH_SHORT).show();
                            editTextname.setText("");
                            break;
                        case "注册失败":
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.
                                    LENGTH_SHORT).show();
                            break;
                        case "注册成功":
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.
                                    LENGTH_SHORT).show();
                            //销毁所有活动
                            ActivityCollector.finishAll();
                            //跳转登录界面
                            Intent intent1 = new Intent(RegisterActivity.this,
                                    MainActivity.class);
                            RegisterActivity.this.startActivity(intent1);
                            break;
                        default:
                            Toast.makeText(RegisterActivity.this, "服务器访问异常",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }

                }
            });
        }

    }
}